//package com.caishi.lkx.common.fliter;
//
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.crypto.symmetric.AES;
//import com.zzw.common.exception.BizException;
//import com.zzw.common.util.SpringMVCUtil;
//import com.caishi.lkx.common.AclResultCode;
//import com.caishi.lkx.common.context.UserContext;
//import com.caishi.lkx.common.ienum.DeviceType;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.annotation.Order;
//
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//
///**
// * @author by keray
// * date:2021/4/1 10:49 上午
// */
//@Slf4j
//@Configuration
//@Order(Integer.MIN_VALUE + 101)
//public class GroupFilter implements Filter {
//
//    private static final AES AES = new AES("05ee4559b3db2ab9a1f5d86a316c90d4".getBytes(StandardCharsets.UTF_8));
//
//    @Resource
//    private UserContext userContext;
//    private static String env;
//
//    @Value("${spring.profiles.active}")
//    public void setEnv(String env) {
//        GroupFilter.env = env;
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        var tokenData = userContext.currentTokenData();
//        Long gid;
//        try {
//            gid = getGroupId(userContext.currentIp(), userContext.getDuid(), (HttpServletRequest) request, (HttpServletResponse) response);
//        } catch (BizException e) {
//            if (userContext.currentTokenData().getDevice() != DeviceType.admin) {
//                removeGroupId((HttpServletResponse) response);
//                SpringMVCUtil.responseWriteWith((HttpServletResponse) response, AclResultCode.localGroupInvalid);
//            }
//            return;
//        }
//        if (gid != null) {
//            tokenData.setLocalGroupId(gid);
//        }
//        chain.doFilter(request, response);
//    }
//
//    public static void removeGroupId(HttpServletResponse response) {
//        var cookie = new Cookie(TokenFilter.TOKEN_USER_GROUP, "");
//        cookie.setMaxAge(0);
//        cookie.setPath("/");
//        cookie.setHttpOnly(true);
//        response.addCookie(cookie);
//    }
//
//    public static String responseSetGroupId(HttpServletResponse response, String ip, Long groupId, String duid, Long expireTime) {
//        var res = AES.encryptBase64(String.format("%s_%s_%d_%d", ip, duid, groupId, expireTime));
//        var cookie = new Cookie(TokenFilter.TOKEN_USER_GROUP, res);
////        cookie.setMaxAge(30 * 24 * 3600);
//        cookie.setPath("/");
//        cookie.setHttpOnly(true);
//        response.addCookie(cookie);
//        return cookie.getValue();
//    }
//
//
//    public static Long getGroupId(String ip, String duid, HttpServletRequest request, HttpServletResponse response) throws BizException {
//        var aesStr = getGid(request);
//        if (StrUtil.isBlank(aesStr)) return null;
//        aesStr = AES.decryptStr(aesStr);
//        var s = aesStr.split("_");
//        if (!GroupFilter.env.equals("dev")) {
//            if (s.length < 4 || !s[0].equals(ip) || !s[1].equals(duid)) {
//                Cookie cookie = new Cookie(TokenFilter.TOKEN_USER_GROUP, null);
//                cookie.setMaxAge(0);
//                cookie.setPath("/");
//                cookie.setHttpOnly(false);
//                response.addCookie(cookie);
//                throw new BizException();
//            }
//        }
//       // var expireTime = Long.parseLong(s[3]);
//        var now = System.currentTimeMillis();
//       // if (expireTime < now) {
//            //throw new BizException();
//       // }
//        return 0l;
//    }
//
//    public static String getGid(HttpServletRequest request) {
//        String aesStr = request.getHeader(TokenFilter.TOKEN_USER_GROUP);
//        if (StrUtil.isBlank(aesStr)) {
//            if (request.getCookies() == null) {
//                return null;
//            }
//            for (Cookie cookie : request.getCookies()) {
//                if (TokenFilter.TOKEN_USER_GROUP.equals(cookie.getName())) {
//                    aesStr = cookie.getValue();
//                }
//            }
//        }
//        return aesStr;
//    }
//
//    public static void main(String[] args) {
////        var aesStr = "wHW5sXpDgNQxblZMdUJmrpJHSnU5Rjha8tjV5K+R0sIZIm1r4zBTIio/BkaiC9i1";
////        System.out.println(AES.decryptStr(aesStr));
//        System.out.println(System.currentTimeMillis());
//        var now = System.currentTimeMillis();
//    }
//
//}
